#!/bin/bash

docker-compose up -d

sleep 20

docker exec -it mysql-master mysql -uroot -p123456 -e 'drop user "slave"@"%";'

docker exec -it mysql-master mysql -uroot -p123456 -e 'CREATE USER "slave"@"%" IDENTIFIED BY "123456";'

docker exec -it mysql-master mysql -uroot -p123456 -e 'GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO "slave"@"%";'

master_status=`docker exec -it mysql-master mysql -uroot -p123456 -e 'show master status;'`
echo "$master_status"  #打印master_status
master_file=`echo "$master_status" | grep "bin"  | awk '{print $2}'`
echo "$master_file"
master_pos=`echo "$master_status" | grep "bin"  | awk '{print $4}'`
echo "$master_pos"

docker exec -it mysql-salve mysql -uroot -p123456 -e 'stop slave;'

docker exec -it mysql-salve mysql -uroot -p123456 -e 'CHANGE MASTER TO MASTER_HOST="mysql-master",MASTER_PORT=3306,MASTER_USER="slave",MASTER_PASSWORD="123456",MASTER_LOG_FILE="'${master_file}'",MASTER_LOG_POS='${master_pos}';'

docker exec -it mysql-salve mysql -uroot -p123456 -e 'start slave;'

docker exec -it mysql-salve mysql -uroot -p123456 -e 'show slave status \G;'


#docker rm -f mysql-master redis mysql-salve


exit
